﻿#include "clientrequest.h"
#include <QJsonDocument>
#include "datacenter.h"
#include <QMessageBox>
#include "onlineusers.h"
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonParseError>
#include <QList>

struct onlineuser
{
    QString account;
    QByteArray photo;
};

ClientRequest::ClientRequest()
{

}

ClientRequest::~ClientRequest()
{

}

void Login::ProcessRequest(ClientHandle *c, const QJsonObject &obj)
{
    qDebug() << "处理登录请求" << endl;
    QString account = obj["account"].toString(); // 从JSON对象obj中提取"account"的字段值，并将其转换为字符串类型
    QString passwd = obj["passwd"].toString(); // 从JSON对象obj中提取"passwd"的字段值，并将其转换为字符串类型

    int res = DataCenter::getInstance()->Login(account, passwd);
    if (res == 1)
    {
        OnlineUsers::getInstance()->AddUsers(account.toStdString(), c);
        QJsonObject response; // 构造响应JSON对象
        response["result"] = 1; // 设置结果状态码为1，表示接收成功
        response["request"] = "LOGIN"; // 当前的请求为LOGIN
        response["msg"] = "success"; // 操作成功的消息
        c->WriteToClient(QJsonDocument(response).toJson()); // 将响应写回客户端
    }
    else if (res == 0)
    {
        QJsonObject response; // 构造响应JSON对象
        response["result"] = 1; // 设置结果状态码为1，表示接收成功
        response["request"] = "LOGIN"; // 当前的请求为LOGIN
        response["msg"] = "password error"; // 密码错误的消息
        c->WriteToClient(QJsonDocument(response).toJson()); // 将响应写回客户端
    }
    else if (res == -1)
    {
        QJsonObject response; // 构造响应JSON对象
        response["result"] = 1; // 设置结果状态码为1，表示接收成功
        response["request"] = "LOGIN"; // 当前的请求为LOGIN
        response["msg"] = "user does not exist"; // 用户不存在的消息
        c->WriteToClient(QJsonDocument(response).toJson()); // 将响应写回客户端
    }
}

void SignUp::ProcessRequest(ClientHandle *c, const QJsonObject &obj)
{
    qDebug() << "处理注册请求" << endl;
    QString account = obj["account"].toString();
    QString passwd = obj["passwd"].toString();
    QString name = obj["name"].toString();
    QString photo = obj["photo"].toString();

    QByteArray imageInfo = QByteArray::fromBase64(photo.toUtf8());

    int res = DataCenter::getInstance()->SignUp(account, passwd, name, imageInfo);
    if (res == 1) // 注册成功
    {
        qDebug() << "注册成功" << endl;
        QJsonObject response; // 构造响应JSON对象
        response["result"] = 1; // 设置结果状态码为1，表示接收成功
        response["request"] = "SIGNUP"; // 当前的请求为SIGNUP
        response["msg"] = "success"; // 操作成功的消息
        c->WriteToClient(QJsonDocument(response).toJson()); // 将响应写回客户端
    }
    else if (res == 0)// 用户名重复导致注册失败
    {
        qDebug() << "用户名重复导致注册失败" << endl;
        QJsonObject response; // 构造响应JSON对象
        response["result"] = 1; // 设置结果状态码为1，表示接收成功
        response["request"] = "SIGNUP"; // 当前的请求为SIGNUP
        response["msg"] = "repeat of name"; // 操作失败的消息
        c->WriteToClient(QJsonDocument(response).toJson()); // 将响应写回客户端
    }
    else // 注册失败
    {
        qDebug() << "注册失败" << endl;
        QJsonObject response; // 构造响应JSON对象
        response["result"] = 1; // 设置结果状态码为1，表示接收成功
        response["request"] = "SIGNUP"; // 当前的请求为SIGNUP
        response["msg"] = "fail"; // 操作失败的消息
        c->WriteToClient(QJsonDocument(response).toJson()); // 将响应写回客户端
    }
}

void OnlineList::ProcessRequest(ClientHandle *c, const QJsonObject &obj)
{
    qDebug() << "处理在线列表请求" << endl;
    QString fromName = obj["fromName"].toString();
    qDebug() << "登录的人:" << fromName << endl;

    QStringList list = OnlineUsers::getInstance()->GetOnlineList();
    QJsonObject response;
    response["result"] = 1;
    response["request"] = "ONLINELIST";

    QList<onlineuser> onlinelist;
    for (int i = 0; i < list.size(); i++)
    {
        onlineuser user;
        user.account = list[i];
        qDebug() << list[i] << endl;
        user.photo = DataCenter::getInstance()->FindPhoto(list[i]);
//        qDebug() << "在线链表头像数据" << user.photo << endl;
        onlinelist.append(user);
    }

    QJsonArray accountListArray;
    for (const onlineuser& user : onlinelist)
    {
        QJsonObject userObj;
        userObj["account"] = user.account;
        // 将 photo 进行 Base64 编码转换为字符串
        userObj["photo"] = QString(user.photo.toBase64());
        accountListArray.append(userObj);
    }

    response["accountlist"] = accountListArray;
    c->WriteToClient(QJsonDocument(response).toJson());
}

Factory::Factory()
{
    requests["LOGIN"] = std::make_shared<Login>();
    requests["SIGNUP"] = std::make_shared<SignUp>();
    requests["ONLINELIST"] = std::make_shared<OnlineList>();
}

requestPtr Factory::getProcess(const QString &request)
{
    return requests[request];
}
